#pragma once
#include "Comm.hpp"

void SetNonBlock(int sock)
{
    int fl = fcntl(sock, F_GETFL);
    if (fl < 0)
    {
        std::cerr << "fcntl erron" << std::endl;
        return;
    }
    fcntl(sock, F_SETFL, fl | O_NONBLOCK);
}

void SplitSegment(std::string &inbuffer, std::vector<std::string> *tokens, const std::string &sep)
{
    while (true)
    {
        int pos = inbuffer.find(sep);
        if (pos == inbuffer.npos)
        {
            break;
        }
        std::string tmp = inbuffer.substr(0, pos);
        tokens->push_back(tmp);
        inbuffer.erase(0, pos + sep.size());
    }
}

bool Deserialize(const std::string &seg, std::string *out1, std::string *out2)
{
    std::string op = "+";
    int pos = seg.find(op);
    if (pos == std::string::npos)
    {
        return false;
    }
    *out1 = std::string(seg.substr(0, pos));
    *out2 = std::string(seg.substr(pos + op.size()));
    return true;
}